<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class LoginController extends AdminBaseController
{
    protected $admin_model;

    public function _initialize() {
        parent::_initialize();
        $this->admin_model = D("Admin");
    }

    /**
     * 登陆页面
     */
    public function index() {
        $admin_id = session('admin.admin_id');
        if (!empty($admin_id)) {//已经登录
            $this->redirect(U("Index/index"));
        }
        $this->display();
    }

    /**
     * 管理员登陆
     */
    public function login() {
        // $verify = new \Think\Verify();
        // if (!$verify->check(I('post.code'))) {
        // $this->error('验证码不正确');
        // }
        $username = I('post.username');
        $password = I('post.password');

        //不含密码
        $info = $this->admin_model->getAdminByUsername($username);

        //密码单独查询
        $db_password = $this->admin_model
            ->where(array('username' => $username))
            ->getField('password');

        if (empty($info) || $db_password != x_encrypt_password($password)) {
            $this->error('用户或密码不正确!',U('index'));
        }
        
        if ($info['status'] != 1) {
            $this->error('用户已被禁止登陆!',U('index'));
        }

        $ip = get_client_ip(); //新的登录ip
        
        // ip于上次登陆不一致则设置is_ip为１
        if (!empty($ip) && !empty($info['last_ip']) && $ip != $info['last_ip']) {
            $this->admin_model->where("admin_id=%d", $info['admin_id'])->save(
                array('is_ip' => 1)
            );
            $info['is_ip'] = 1;
        }

        $this->admin_model->where("admin_id=%d", $info['admin_id'])->save(
            array(
                'last_time' => NOW_TIME,
                'last_ip' => $ip
            )
        );
        
        session('admin', $info);
        $this->success('登录成功', U('Admin/Index/index'));
    }

    /**
     * 管理员退出
     */
    public function logout() {
        // 退出的时候值修改为0，只有登录时IP不一样时修改
        $adminInfo = session('admin');
        if (!empty($adminInfo)) {
            $this->admin_model->where(array('admin_id' => $adminInfo['admin_id']))->save(
                array('is_ip' => 0)
            );
            session('admin', null);
            $this->success('退出成功', U('Login/index'));
        } else {
            $this->error('没有登陆,请登陆', U('Login/index'));
        }
    }

    /**
     * 验证码
     */
    public function verifyImg() {
        $config = array(
            'imageW'   => 150,
            'imageH'   => 40,
            'fontSize' => 18,
            'length'   => 4,
            'fontttf'  => '5.ttf'
        );
        $verify = new \Think\Verify($config);
        $verify->entry();
    }
}